package com.share.homestay.web;

import com.share.homestay.entity.OrderLogs;
import com.share.homestay.entity.SysUser;
import com.share.homestay.pojo.JsonResult;
import com.share.homestay.pojo.LoginUser;
import com.share.homestay.service.SysUserServiceFeign;
import com.share.homestay.utils.JwtUtils;
import com.share.homestay.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理")
@RestController
@CrossOrigin
@RequestMapping("sysUser")
public class SysUserController {
    @Autowired
    private SysUserServiceFeign sysUserServiceFeign;

    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("sysUserPageQuery")
    @ApiOperation(value = "用户分页条查",httpMethod = "POST",notes = "用户分页进行条查")
    JsonResult sysUserPageQuery(@RequestBody SysUser sysUser,@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "100")Integer pageSize){
        return sysUserServiceFeign.sysUserPageQuery(sysUser,pageNum,pageSize);
    }

    @RequestMapping("selectPermsByUserId")
    @ApiOperation(value = "通过用户id查找用户的权限",httpMethod = "POST")
    JsonResult selectPermsByUserId(String token){
        String myToken = JwtUtils.checkToken(token);
        LoginUser loginUser = (LoginUser) redisUtil.get("login:" + myToken);
        return JsonResult.success(loginUser.getMenus());
    }

    @RequestMapping("selectLogs")
    @ApiOperation(value = "条件查询的日志信息",httpMethod = "POST")
    JsonResult selectLogs(@RequestBody OrderLogs orderLogs,@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "100")Integer pageSize){
        return sysUserServiceFeign.selectLogs(orderLogs,pageNum,pageSize);
    }

    @RequestMapping("findAllUser")
    @ApiOperation(value = "查询全部用户信息",httpMethod = "GET")
    List<SysUser> findAllUser(){
        return sysUserServiceFeign.findAllUser();
    }
    @RequestMapping("findUserById")
    @ApiOperation(value = "获取登录的用户信息")
    JsonResult findUserById(String token){
        String myToken = JwtUtils.checkToken(token);
        LoginUser loginUser = (LoginUser) redisUtil.get("login:" + myToken);
        return JsonResult.success(loginUser.getSysUser());
    }
}
